package th.kmutt.se.is;

import th.kmutt.se.is.fact.adapter.ChargesAdapter;
import th.kmutt.se.is.model.Charges;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ListView;

public class ChargesList extends Fragment implements OnClickListener{
	
	View view;
	ListView list;
	ImageButton btnMenu1, btnMenu2;
	
	Charges chargesListData[], charges;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.screen_charges_list, container,false);
		init();
		return view;		
	}
	
	private void init(){
		
		charges = new Charges(getActivity());

		btnMenu1 = (ImageButton) view.findViewById(R.id.btnMenu1);
		btnMenu2 = (ImageButton) view.findViewById(R.id.btnMenu2);
		list = (ListView) view.findViewById(R.id.chargesList);
		
		if(btnMenu1!=null)btnMenu1.setOnClickListener(this);
		btnMenu2.setOnClickListener(this);
		
		process();
	}
	
	private void process(){
		chargesListData = charges.getChargesList(0);
		if(chargesListData != null){
			ChargesAdapter adapter = new ChargesAdapter(getActivity(), R.layout.row_charges, chargesListData);
			list.setAdapter(adapter);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btnMenu1:{
				((Main)getActivity()).openDrawer();
			}break;
			case R.id.btnMenu2:{
				Intent intent = new Intent(getActivity(), ChargesAdd.class);
				startActivity(intent);
			}break;
		}
	}
	
	
}
